<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>新闻列表</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link href="/resources/css/base.css" rel="stylesheet"
	type="text/css" />
<link href="/resources/js/miniui/themes/default/miniui.css"
	rel="stylesheet" type="text/css" />
<script src="/resources/js/jquery-1.6.2.min.js"
	type="text/javascript"></script>
<script src="/resources/js/miniui/miniui.js"
	type="text/javascript"></script>
<script src="/resources/js/ckeditor/ckeditor.js"
	type="text/javascript"></script>
<style type="text/css">
.errorText {
	color: red;
}
#editWindow {top: 50px !important;left:150px!important}
.td1 {
	width: 20%;
	text-align: right;
}

.td2 {
	width: 30%;
	text-align: left;
}

.td3 {
	width: 12%;
	text-align: right;
}

.td4 {
	width: 38%;
	text-align: left;
}
</style>
</head>
<body>
	<h1>新闻列表</h1>
	<div style="padding-top: 5px; padding-bottom: 5px; width: 95%;">
		<a class="mini-button"
			img="/resources/js/miniui/themes/icons/add.gif"
			onclick="load4add()">增加</a> <a class="mini-button"
			img="/resources/js/miniui/themes/icons/edit.gif"
			onclick="editRow()">修改</a> <a class="mini-button"
			img="/resources/js/miniui/themes/icons/remove.gif"
			onclick="delRow()">删除</a>
	</div>
	<div style="padding-top: 5px; padding-bottom: 5px; width: 95%;">
		<span>新闻标题：</span><input type="text" id="search_title" />
		<span>新闻类型：</span><input id="search_type" class="mini-combobox" valueField="code" textField="data"
						valueFromSelect="true" url="/news/getTypeJson" />
		<input type="button" value="查找" onclick="search()" />
	</div>
	<div id="mini-datagrid" class="mini-datagrid"
		style="width: 95%; height: 400px;"
		url="/news/selectNewsListByPage" pageSize="20" idField="id"
		allowResize="true">

		<div property="columns">
			<div type="indexcolumn">序号</div>
			<div type="checkcolumn">选择</div>
			<div field="title">新闻标题</div>
			<div field="type">新闻类型</div>
			<div field="auther">作者</div>
			<div field="status">状态</div>
			<div field="createTime" dateFormat="yyyy-MM-dd HH:mm:ss">发布时间</div>
		</div>
	</div>
	
	<div id="editWindow" class="mini-window" title="新闻窗口"
		style="width: 750px;left:100px;" showModal="true" allowResize="true"
		allowDrag="true">
		<div id="editform" class="form">
			<input class="mini-hidden" name="id" value="0" />
			<table style="width: 100%;">
				<tr>
					<td class="td1">新闻标题：</td>
					<td class="td2"><input name="title"
						class="mini-textbox" maxLength=200 /></td>
					<td class="td3">新闻类型：</td>
					<td class="td4"><input name="type" class="mini-combobox" valueField="code" textField="data"
						valueFromSelect="true" url="/news/getTypeJson" /></td>
				</tr>
				<tr>
					<td class="td1">作者：</td>
					<td class="td2"><input name="auther"
						class="mini-textbox" maxLength=200 /></td>
					<td class="td3">状态：</td>
					<td class="td4"><input name="status" class="mini-combobox" valueField="code" textField="data"
					 valueFromSelect="true" url="/news/getStatusJson"/></td>
				</tr>
				<tr>
					<td class="td1">内容：</td>
					<td colspan="3">
						<textarea name="content" id="content"></textarea>
					<!-- <input name="content" class="mini-textarea" style="width:343px;height:60px;"/> --></td>
				</tr>
				<tr>
					<td
						style="text-align: center; padding-top: 5px; padding-right: 20px;"
						colspan="6"><a class="mini-button"
						img="/resources/js/miniui/themes/icons/save.gif"
						onclick="saveRow()">保存</a> <a class="mini-button"
						img="/resources/js/miniui/themes/icons/cancel.gif"
						onclick="cancelRow()">取消</a></td>
				</tr>

			</table>
		</div>
	</div>
</body>
<script type="text/javascript">
	mini.parse();
	var datagrid = mini.get("mini-datagrid");
	var editWindow = mini.get("editWindow");
	var form = new mini.Form("editform");
	var actionType = "insertNews";
	datagrid.load();
	
	CKEDITOR.replace('content');
	
	function load4add() {
		actionType = "insertNews";
		editWindow.show();
		form.clear();
		CKEDITOR.instances.content.setData("");
	}
	
	function saveRow() {
		var json = form.getData();
		json.content = CKEDITOR.instances.content.getData();
		if (json.id == 'undefined' || json.id == '') {
			json.id = 0;
		}
		/* var content = CKEDITOR.instances.content.getData();
		console.log(content); */
		$.ajax({
			url : "/news/" + actionType,
			data : json,
			type : "post",
			success : function(text) {
				editWindow.hide();
				datagrid.reload();
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert(jqXHR.responseText);
			}
		});

	}

	function cancelRow() {
		datagrid.reload();
		editWindow.hide();
	}
	
	function editRow() {

		var node = datagrid.getSelected();
		if (node) {
			actionType = "updateNews";
			editWindow.show();
			form.clear();
			form.setData(node);
			CKEDITOR.instances.content.setData(node.content);
		} else {
			mini.alert("请选择一条!");
		}
	}
	
	function delRow() {

		var node = datagrid.getSelected();
		if (node) {
			mini.confirm("确定删除！", "提示", function(retCode) {
				if (retCode == 'ok') {
					// dosomething 删除
					$.ajax({
						url : "/news/deleteNews",
						data : {
							id : node.id
						},
						type : "post",
						success : function(text) {
							editWindow.hide();
							datagrid.reload();
						},
						error : function(jqXHR, textStatus, errorThrown) {
							alert(jqXHR.responseText);
						}
					});

				}
			})
		} else {
			mini.alert("请选择一条!");
		}
	}
	
	function search(){
		datagrid.load({title: $('#search_title').val(),
				type : mini.get("#search_type").getValue()});
	}

</script>

</html>
